//引入模型
require('./../model/errorsModel');
const mongoose = require('mongoose');

//获取所有的错题
async function getAllErrors(req, res, next) {
    let studentId = "610bb559a50c000097006402";
    let errors = await mongoose.model('errorsModel').find({ studentId: studentId }).populate("exerciseId");
    // let errors = await mongoose.model('errorsModel').find({studentId:req.body}).populate("exerciseId");
    console.log(errors);
    res.send({ data: errors, code: 200 });
}
//删除错题
async function removeError(req, res, next) {
    let id = "619b2a5cae0cc5505ce58d8b";
    let { deletedCount } = await mongoose.model('errorsModel').deleteOne({ _id: id });
    // let {deletedCount} = await mongoose.model('errorsModel').deleteOne({_id:req.body});
    if (deletedCount > 0) {
        res.send({ code: 200 })
    } else {
        res.send({ code: -1 })
    }
}
//新增错题
async function addError(req, res, next) {
    let array = req.body;
    let data = "";
    for (let i = 0; i < array.length; i++) {
        data = await mongoose.model("errorsModel").create({ exerciseId: array[i].exerciseId, studentId: array[i].studentId, errorAnswer: array[i].errorAnswer });
    }
    if (data != "") {
        res.send({ code: 200 })
    } else {
        res.send({ code: -1 })
    }

}

module.exports = {
    getAllErrors,
    addError,
    removeError
}